package com.trs.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

public class Util {

	// 博杉内容解密
	// 读取配置文件
	public static String getPropertiesVal(String vStr) {

		Properties props = null;
		try {
			props = new Properties();
			InputStream in = Util.class.getResourceAsStream("hy.properties");
			BufferedReader bf = new BufferedReader(new InputStreamReader(in));
			props.load(bf);
			in.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return props.getProperty(vStr);
	}

	/**
	 * 正则替换所有特殊字符
	 * 
	 * @param orgStr
	 * @return
	 */
	public static String replaceSpecStr(String orgStr) {
		if (null != orgStr && !"".equals(orgStr.trim())) {
			String regEx = "[\\s~·`!！@#￥$%^……&*（()）\\-——\\-=+【\\[\\]】｛{}｝\\|、\\\\；;：:‘'“”\"，,《<。.》>、/？?]";
			Pattern p = Pattern.compile(regEx);
			Matcher m = p.matcher(orgStr);
			orgStr = m.replaceAll("");
			return orgStr.replaceAll("\\s*", "");
		}
		return null;
	}

	/**
	 * 判断是否为空
	 * 
	 * @param string---字符串
	 * @return ture、false
	 */
	public static boolean isNull(String string) {
		if (string != null) {
			string = string.trim();
		}

		if (string == null || string.equals("") || string.equals("null")) {
			return true;
		} else {
			return false;
		}
	}

	public static String deal(String string) {
		return isNull(string) ? "" : string;
	}

	// 切换urlip为配置文件内可访问的接口ip
	public static String changeWIp(String nUrl) {
		String wUrl = Util.getPropertiesVal("WCM_HOST_URL");
		java.net.URL url = null;
		try {
			url = new java.net.URL(wUrl);
			String wIp = url.getHost();
			url = new java.net.URL(nUrl);
			String nIp = url.getHost();
			return nUrl.replace(nIp, wIp);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

		return "";
	}

	// 判断是否为文件链接
	public static boolean isFile(String url) {
		String str = url.toLowerCase();
		if (str.endsWith(".doc") || str.endsWith(".docx") || str.endsWith(".xls") || str.endsWith(".xlsx") || str.endsWith(".zip") || str.endsWith(".rar") || str.endsWith(".pdf")) {
			return true;
		}
		return false;
	}

	// 判断是否为图片链接
	public static boolean isImg(String url) {
		String str = url.toLowerCase();
		if (str.endsWith(".jpg") || str.endsWith(".gif") || str.endsWith(".png")) {
			return true;
		}
		return false;
	}

	// 判断是否为视频链接
	public static boolean isMedia(String url) {
		String str = url.toLowerCase();
		if (str.endsWith(".avi") || str.endsWith(".mp4") || str.endsWith(".mp3") || str.endsWith(".flv") || str.endsWith(".wmv") || str.endsWith(".mpg")) {
			return true;
		}
		return false;
	}

	// 判断是否为flash链接
	public static boolean isFlash(String url) {
		String str = url.toLowerCase();
		if (str.endsWith(".swf")) {
			return true;
		}
		return false;
	}

	// rs转map
	public static Map<String, Object> convertMap(ResultSet rs) {
		Map<String, Object> map = new TreeMap<String, Object>();
		try {
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			while (rs.next()) {
				for (int i = 1; i <= columnCount; i++) {
					map.put(md.getColumnName(i), rs.getObject(i));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return map;
	}

	public static List<Map<String, Object>> convertList(ResultSet rs) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			ResultSetMetaData md = rs.getMetaData();
			int columnCount = md.getColumnCount();
			while (rs.next()) {
				Map<String, Object> rowData = new HashMap<String, Object>();
				for (int i = 1; i <= columnCount; i++) {
					rowData.put(md.getColumnName(i), rs.getObject(i));
				}
				list.add(rowData);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				rs = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	/**
	 * 
	 * @param content---内容
	 * @return content---内容
	 */
	public static String changeContent(String content) {

		content = content.replace("&amp;lt;", "<");
		content = content.replace("&amp;gt;", ">");
		content = content.replace("&amp;#39;", "\'");
		content = content.replace("&amp;quot;", "\"");
		content = content.replace("&amp;amp;nbsp;", "&nbsp;");

		content = content.replace("&lt;", "<");
		content = content.replace("&gt;", ">");
		content = content.replace("&#39;", "\'");
		content = content.replace("&quot;", "\"");
		content = content.replace("&amp;nbsp;", "&nbsp;");
		content = content.replace("&amp;ensp;", "&ensp;");

		content = content.replace("--- #turnpageflag# ---", "<p></p>");
		content = content.replace("<A", "<a");
		content = content.replace("</A>", "</a>");
		content = content.replace("<IMG", "<img");
		content = content.replace("<IFRAME", "<iframe");
		content = content.replace("</IFRAME>", "</iframe>");
		content = content.replace("<VIDEO", "<video");
		content = content.replace("</VIDEO>", "</video>");
		content = content.replace("<EMBED", "<embed");
		content = content.replace("</EMBED>", "</embed>");
		content = content.replace("<IMAGE", "<img");
		content = content.replace("SRC=", "src=");
		content = content.replace("dir>", "div>");
		content = content.replace("vsb", "");
		content = content.replaceAll("(/upload/U[0-9]{7}/U[0-9]{9}/U[0-9]{21}.[a-z]{3,4})", "");
		content = content.replaceAll("(<object[\\s]+classid=\".+?\" id=ieooui></object>)", "");

		return content;
	}

	public static void mapToString(Map<String, Integer> map) {
		for (Entry<String, Integer> entry : map.entrySet()) {
			System.out.println("Key = " + entry.getKey() + ",value=" + entry.getValue());
		}
	}

	public static void mapListToString(List<Map<String, Object>> list) {
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map = list.get(i);
			for (Entry<String, Object> entry : map.entrySet()) {
				System.out.println("Key = " + entry.getKey() + ",value=" + entry.getValue());
			}
		}
	}

	public static boolean isNull(Object string) {
		@SuppressWarnings("deprecation")
		String str = ObjectUtils.toString(string, "");
		return StringUtils.isBlank(str);
	}

	public static void mapToString1(Map<String, Object> map) {
		for (Entry<String, Object> entry : map.entrySet()) {
			System.out.println("Key = " + entry.getKey() + ",value=" + entry.getValue());
		}

	}

}
